package com.itheima.manager.web;

import com.itheima.manager.pojo.User;
import com.itheima.manager.service.ILoginService;
import com.itheima.manager.service.imp.LoginServiceImp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    private ILoginService iLoginService = new LoginServiceImp();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remember = request.getParameter("remember");









        User user = iLoginService.login(username, password);

        if (user!=null){

            if ("yes".equals(remember)){
                Cookie c_username = new Cookie("username", username);
                Cookie c_password = new Cookie("password", password);
                c_username.setMaxAge(60*60*24*7);
                c_password.setMaxAge(60*60*24*7);

                response.addCookie(c_username);
                response.addCookie(c_password);
            }


            request.getRequestDispatcher("/ListServlet").forward(request,response);
        }else {
            request.setAttribute("loginerr","登录失败");
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             doGet(request,response);
    }
}
